package com.me.forum.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.me.forum.dao.IRemarkDao;
import com.me.forum.domain.Page;
import com.me.forum.domain.Remark;
import com.me.forum.domain.User;
import com.me.forum.request.RemarkRequest;
import com.me.forum.service.IRemarkService;
import com.me.forum.utils.TypeConversion;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class RemarkService extends BaseService<IRemarkDao, Remark> implements IRemarkService {
	@Autowired
	private PageService pageService;
	
	@Autowired
	private UserService userService;

	@Autowired
	private PlateService plateService;
	
	/**
	*@description:帖子中添加评论接口
	*@author:LWP
	*@createTime:2020-04-26 19:46
	*/
	@Override
	public boolean addRemark(RemarkRequest rreq) {
		if (!rreq.isLegal()) {//判断前端传入数据是否合法
			return false;
		}
		String userId = rreq.getUserId();
		String pageId = rreq.getPageId();
		
		User user = userService.findById(userId);
		Page page = pageService.findById(pageId);
		if (user == null || page == null) {//判断用户和帖子是否存在
			return false;
		}

		Remark remark = new Remark();
		remark.setContent(rreq.getContent());
	    remark.setUserId(Long.valueOf(userId));
		remark.setPageId(Long.valueOf(pageId));

	    return save(remark);
	}


	//获取评论 by Orange
	@Override
	public IPage<Remark> getAll(RemarkRequest rreq, String _page, String _size) {
		Long page = TypeConversion.toLong(_page);
		Long size = TypeConversion.toLong(_size);
		
		return dao.selectByAll(new com.baomidou.mybatisplus.extension.plugins.pagination.Page(page,size),rreq);
	}

	@Override
	public boolean updateById(RemarkRequest rreq) {
		if (!rreq.isLegal()) {
			return false;
		}
		return dao.updateById(rreq) == 1;
	}

	@Override
	public IPage<Remark> getRemarks(RemarkRequest rreq, String page, String size) {
		return dao.selectByAll(new com.baomidou.mybatisplus.extension.plugins.pagination.Page(TypeConversion.toLong(page),TypeConversion.toLong(size)),rreq);
	}


}
